<?php
include('../mysqli_connect.php');
$sqlGender1 = "select count(*) male from user where gender = 1;";
$resGender1 = mysqli_query($dbc, $sqlGender1);
$sqlGender2 = "select count(*) female from user where gender = 2;";
$resGender2 = mysqli_query($dbc, $sqlGender2);

$sqlAge1 = "select count(*) from user where age BETWEEN 7 and 12;";
$resAge1 = mysqli_query($dbc, $sqlAge1);
$sqlAge2 = "select count(*) from user where age BETWEEN 13 and 18;";
$resAge2 = mysqli_query($dbc, $sqlAge2);
$sqlAge3 = "select count(*) from user where age BETWEEN 19 and 29;";
$resAge3 = mysqli_query($dbc, $sqlAge3);
$sqlAge4 = "select count(*) from user where age BETWEEN 30 and 49;";
$resAge4 = mysqli_query($dbc, $sqlAge4);
$sqlAge5 = "select count(*) from user where age BETWEEN 50 and 64;";
$resAge5 = mysqli_query($dbc, $sqlAge5);
$sqlAge6 = "select count(*) from user where age>65";
$resAge6 = mysqli_query($dbc, $sqlAge6);

$sqlMap = "select address,count(address) from user group by address;";
$resMap = mysqli_query($dbc, $sqlMap);
$mapArray = array();
$i = 0;
while ($row = mysqli_fetch_row($resMap)) {
  $mapArray[$i]['name'] = $row[0];
  $mapArray[$i]['value'] = $row[1];
  $i = $i + 1;
}

if (!$resGender1 || !$resGender2) {
  printf("Error: %s\n", mysqli_error($dbc));
  $response = array('status' => false, 'error' => mysqli_error($dbc));
  echo json_encode($response);
  exit();
}
$male = mysqli_fetch_assoc($resGender1);
$female = mysqli_fetch_assoc($resGender2);

$ageArray = array();
$res1 = mysqli_fetch_assoc($resAge1);
$ageArray[] = $res1;
$res2 = mysqli_fetch_assoc($resAge2);
$ageArray[] = $res2;
$res3 = mysqli_fetch_assoc($resAge3);
$ageArray[] = $res3;
$res4 = mysqli_fetch_assoc($resAge4);
$ageArray[] = $res4;
$res5 = mysqli_fetch_assoc($resAge5);
$ageArray[] = $res5;
$res6 = mysqli_fetch_assoc($resAge6);
$ageArray[] = $res6;


$response = array('status' => true, 'male' => $male, 'female' => $female, 'age' => $ageArray, 'map' => $mapArray);
echo json_encode($response);
